package ru.ifmo.swing.genetics.automaton;

import ru.ifmo.automaton.Automaton;
import ru.ifmo.automaton.SynchronizingAutomaton;
import ru.ifmo.common.Source;
import ru.ifmo.swing.EvolutionControlWithConfigProvider;
import ru.ifmo.swing.genetics.config.common.Chooser;
import ru.ifmo.swing.genetics.config.common.ChooserListener;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author avhaliullin
 */
public class AutomatonChooser<C, S> implements Source<Automaton<C, S>>, EvolutionControlWithConfigProvider {
    private Source<Automaton<C, S>> source;
    private Chooser<Source<Automaton<C, S>>> control;

    public AutomatonChooser(Map<String, Source<Automaton<C, S>>> options) {
        control = new Chooser<Source<Automaton<C, S>>>(options);
        control.addListener(new ChooserListener<Source<Automaton<C, S>>>() {
            public void chosen(Source<Automaton<C, S>> automatonSource) {
                source = automatonSource;
            }
        });
    }

    public Automaton<C, S> create() {
        return source.create();
    }

    public JComponent getControl() {
        return control;
    }

    public void reset() {
    }

    public void setDescription(String description) {
        control.setTitle(description);
    }

    public Map<String, Object> getParamValues() {
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("Selected automaton", control.getItemText());
        res.put("Selected automaton properties", control.getItem());
        Automaton<C, S> automaton = create();
        if (automaton instanceof SynchronizingAutomaton<?, ?>) {
            res.put("MSW length", ((SynchronizingAutomaton<?, ?>) automaton).mswLength());
        }
        return res;
    }
}
